//
//  SwitchViewController.m
//  10702
//
//  Created by student on 16/2/22.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "SwitchViewController.h"
#import "First.h"
#import "Second.h"
#import "Third.h"

@interface SwitchViewController ()

@property (nonatomic,strong) First *firstVC;
@property (nonatomic,strong) Second *secondVC;
@property (nonatomic,strong) Third *thirdVC;

@property (nonatomic,assign) int num;


@end

@implementation SwitchViewController

// 懒加载 节省内存
// 重写getter方法
// 第一次用到这个对象时候去创建这个对象，如果始终没用到这个对象,则不会创建,节省了内存

- (First *)firstVC
{
    if (_firstVC == nil) {
        _firstVC = [[First alloc]init];
    }
    return _firstVC;
}

- (Second *)secondVC
{
    if (!_secondVC) {
        _secondVC = [[Second alloc]init];
    }
    return _secondVC;
}

- (Third *)thirdVC
{
    if (!_thirdVC) {
        _thirdVC = [[Third alloc]init];
    }
    return _thirdVC;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self switchNum:nil];
}

- (IBAction)switchNum:(id)sender {
    int tem = 1;
    [UIView beginAnimations:@"ViewFlip" context:nil];
    [UIView setAnimationDuration:1.25];//动画时间
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];//运动方式
    if (self.num<tem) {
        [self.view insertSubview:self.firstVC.view atIndex:0];
        [self.secondVC.view removeFromSuperview];
        [self.thirdVC.view removeFromSuperview];
        //动画的样式
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
    }
    else if (self.num == tem)
    {
        [UIView setAnimationDuration:0];
    }
    else
    {
        [self.view insertSubview:self.firstVC.view atIndex:0];
        [self.secondVC.view removeFromSuperview];
        [self.thirdVC.view removeFromSuperview];
        //动画的样式
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
    }
    [UIView commitAnimations];//动画开始播放
    self.num = 1;
}
- (IBAction)sencond:(id)sender {
    int tem = 2;
    [UIView beginAnimations:@"ViewFlip" context:nil];
    [UIView setAnimationDuration:1.25];//动画时间
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];//运动方式
    if (self.num<tem) {
        [self.view insertSubview:self.secondVC.view atIndex:0];
        [self.firstVC.view removeFromSuperview];
        [self.thirdVC.view removeFromSuperview];
        //动画的样式
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
    }
    else if (self.num == tem)
    {
        [UIView setAnimationDuration:0];
    }
    else
    {
        [self.view insertSubview:self.secondVC.view atIndex:0];
        [self.firstVC.view removeFromSuperview];
        [self.thirdVC.view removeFromSuperview];
        //动画的样式
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
    }
    [UIView commitAnimations];//动画开始播放
    self.num = 2;

}
- (IBAction)third:(id)sender {
    int tem = 3;
    [UIView beginAnimations:@"ViewFlip" context:nil];
    [UIView setAnimationDuration:1.25];//动画时间
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];//运动方式
    if (self.num<tem) {
        [self.view insertSubview:self.thirdVC.view atIndex:0];
        [self.secondVC.view removeFromSuperview];
        [self.firstVC.view removeFromSuperview];
        //动画的样式
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
    }
    else if (self.num == tem)
    {
        [UIView setAnimationDuration:0];
    }
    else
    {
        [self.view insertSubview:self.thirdVC.view atIndex:0];
        [self.secondVC.view removeFromSuperview];
        [self.firstVC.view removeFromSuperview];
        //动画的样式
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
    }
    [UIView commitAnimations];//动画开始播放
    self.num = 3;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
